package vn.hanelsoft.newsduet.control;

import java.util.ArrayList;

import vn.hanelsoft.newsduet.R;
import vn.hanelsoft.newsduet.common.ConstCommon;
import vn.hanelsoft.newsduet.common.FacebookUtility;
import vn.hanelsoft.newsduet.model.adapter.AdapterFlipview;
import vn.hanelsoft.newsduet.model.object.ObjectCategory;
import vn.hanelsoft.newsduet.model.object.ObjectNews;
import vn.hanelsoft.newsduet.view.PopupLayout;
import vn.hanelsoft.newsduet.view.PopupLayout.SelectItem;
import vn.hanelsoft.newsduet.view.ShareTwitterDialog;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.aphidmobile.flip.FlipCards;
import com.aphidmobile.flip.FlipViewController;
import com.aphidmobile.flip.FlipViewController.ViewFlipListener;
import com.google.android.gms.plus.PlusShare;

public class DetailActivity extends Activity implements OnClickListener {
	Button btn_back, btn_share;
	private FlipViewController flipView;
	ArrayList<ObjectCategory> list_detail;
	TextView txt_category, txt_news_name, txt_news_date, txt_news_sumary,
			txt_news_description;
	int position_page = 0;
	AdapterFlipview adapterFlipView;

	PopupLayout popupLayout;
	PopupWindow pw;

	public static final String KEY_FACEBOOK = "508278855920267";

	LinearLayout lnFace, lnTwiter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.child_detail);
		initWidget();
	}

	/**
	 * Called when the activity is first created.
	 */
	@SuppressWarnings("unchecked")
	private void initData() {
		try {
			Bundle bundle = getIntent().getExtras();
			list_detail = (ArrayList<ObjectCategory>) bundle
					.getSerializable(ConstCommon.DETAIL_DATA_LIST_OBJECT);
			position_page = bundle.getInt(ConstCommon.DETAIL_DATA_POSITION);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void setSize() {
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		float height = displaymetrics.heightPixels;
		float width = displaymetrics.widthPixels;

		FlipCards.height = height;
		FlipCards.width = width;
	}

	public void initWidget() {
		setSize();
		flipView = (FlipViewController) findViewById(R.id.flipView);
		adapterFlipView = new AdapterFlipview(DetailActivity.this, list_detail);
		flipView.setAdapter(adapterFlipView, position_page);
		txt_category = (TextView) findViewById(R.id.item_detail_text_title);
		popupLayout = new PopupLayout(this);

		popupLayout.setItemSelect(new SelectItem() {

			@Override
			public void selectTwitter() {
				shareTwitter();
			}

			@Override
			public void selectGoogle() {
				shareGoogle();
			}

			@Override
			public void selectFacebook() {
				shareFacebook();
			}
		});

		btn_back = (Button) findViewById(R.id.item_detail_button_back);
		btn_share = (Button) findViewById(R.id.item_detail_button_share);
		btn_back.setOnClickListener(this);
		btn_share.setOnClickListener(this);

		showCurrentPage(position_page);

		flipView.setOnViewFlipListener(new ViewFlipListener() {

			@Override
			public void onViewFlipped(View view, int position) {
				if (position_page != position) {
					position_page = position;
					showCurrentPage(position);
				}
			}
		});
	}

	public ObjectNews getItem(int position) {
		int i = 0;
		int j = position;
		for (; i < list_detail.size(); i++) {
			int size = list_detail.get(i).getCategory_listNews().size();
			if (j >= size) {
				j -= size;
			} else {
				break;
			}
		}
		return list_detail.get(i).getCategory_listNews().get(j);
	}

	public void showCurrentPage(int page) {
		ObjectNews obj_new = getItem(page);
		txt_category.setText(obj_new.getNews_id_category());

		if (page != 0 && page != 1) {
			if (page > 1 && page < (list_detail.size() - 2)) {
				page = 2;
			} else if (page == list_detail.size() - 2) {
				page = 3;
			} else {
				page = 4;
			}

		}
	}

	protected void shareFacebook() {
		pw.dismiss();
		// String APPLICATION_ID = "569988706397716";
		FacebookUtility facebook = new FacebookUtility(this, KEY_FACEBOOK);
		Bundle params = new Bundle();
		params.putString("title", getItem(position_page).getNews_link());
		params.putString("link", getItem(position_page).getNews_link());
		facebook.postToWall(params);
	}

	protected void shareGoogle() {
		pw.dismiss();
		 Intent shareIntent = new PlusShare.Builder(this)
		 .setType("text/plain")
		 .setText(getItem(position_page).getNews_link())
		 .setContentUrl(Uri.parse("https://developers.google.com/+/"))
		 .getIntent();
		 startActivityForResult(shareIntent, 0);
	}

	protected void shareTwitter() {
		pw.dismiss();
		String TWITTER_URL = "https://mobile.twitter.com/home?status=";
		ShareTwitterDialog shareTwitterDialog = new ShareTwitterDialog(this,
				TWITTER_URL + getItem(position_page).getNews_link());
		shareTwitterDialog.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		shareTwitterDialog.show();
	}

	protected void showPopupWindow() {
		pw = new PopupWindow(getApplicationContext());
		pw.setTouchable(true);
		pw.setFocusable(true);
		pw.setOutsideTouchable(true);
		pw.setTouchInterceptor(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					pw.dismiss();
					return true;
				}
				return false;
			}
		});

		pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
		pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
		pw.setOutsideTouchable(false);
		pw.setContentView(popupLayout);
		pw.showAsDropDown(btn_share, 0, 20);

	}

	public void shareNews() {
		showPopupWindow();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.item_detail_button_share:
			shareNews();
			break;
		case R.id.item_detail_button_back:
			finish();
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		flipView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		flipView.onPause();
	}
}
